/*******************************************************************************
 * Copyright (c) 2012 Clever Devices.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 * 	Clever Devices Interns - 
 * 	Alex Frankel, Andria Young, Kevin Thomas, Nicholas Culpepper, Taner Oztimurlenk, Thomas Volberg
 ******************************************************************************/
package com.framework.fares;

import com.framework.bustime.ArrivalsGrid;
import com.framework.cleverstop_tentative.BusTimeMainActivity;
import com.framework.cleverstop_tentative.R;
import com.framework.rewards.Achievements;
import com.framework.rewards.Ridership;
import com.slidingmenu.lib.app.SlidingActivity;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.framework.cleverstop_tentative.BusTimeMainActivity;
import com.framework.cleverstop_tentative.R;
import com.framework.rewards.Ridership;

public class FarePaymentActivity extends Activity implements OnClickListener{
	
	//This will be the notification that pops up when the accept payment button is pushed
		
	NotificationManager nm;
	static final int uniqueID = 1394885;
	TextView charge, track;
	Button yes, no;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// SET CONTENT AND REGISTER ACCEPT AND DECLINE BUTTONS TO LISTENERS
		setContentView(R.layout.activity_nfc);
		Button accept = (Button)findViewById(R.id.acceptButton);
		accept.setOnClickListener(this);
		Button decline = (Button)findViewById(R.id.declineButton);
		
		//Decline button is pressed
		decline.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
					Intent lostIntent = new Intent(FarePaymentActivity.this, BusTimeMainActivity.class);
					startActivity(lostIntent);
					finish();
		
			//Crunchy Toast
			
					LayoutInflater inflater = getLayoutInflater();
					View layout = inflater.inflate(R.layout.customer_lost_notification, (ViewGroup) findViewById(R.id.toast_layout_root));
					ImageView image = (ImageView) layout.findViewById(R.id.ivLostNotif);
					image.setImageResource(R.drawable.ic_notification);
					TextView text = (TextView) layout.findViewById(R.id.tvLostNotif);
					text.setText("Payment Declined");
					Context contextLost = getApplicationContext();
					Toast toastLost = new Toast(getApplicationContext());
					toastLost.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
					toastLost.setDuration(Toast.LENGTH_LONG);
					toastLost.setView(layout);
					toastLost.show();
			}
		});
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		nm.cancel(uniqueID);
		
		
		Drawable d = this.getResources().getDrawable(R.drawable.layout_background);
		getActionBar().setBackgroundDrawable(d);
		getActionBar().setHomeButtonEnabled(true);
	}
		
	public void onClick(View v) {
		
		//finish notification
		Intent intent = new Intent(this, Ridership.class);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
		String body = "Congratulations! You have just saved one ticket";
		String title = "Go Green, Save Green";
		Notification n = new Notification(R.drawable.greennoti, body, (System.currentTimeMillis()));
		n.setLatestEventInfo(this, title, body, pi);
		n.defaults = Notification.DEFAULT_ALL;
		nm.notify(uniqueID, n);
	
	//This is the dialog
	final Dialog d = new Dialog(this);
	d.setContentView(R.layout.activity_nfcdialog);
	d.setTitle("Confirmed");
	charge = (TextView) d.findViewById(R.id.tvChargeConfirm);
	track = (TextView) d.findViewById(R.id.tvTrackRide);
	yes = (Button) d.findViewById(R.id.bTrackYes);
	no = (Button) d.findViewById(R.id.bTrackNo);
	d.show();
	yes.setOnClickListener(new View.OnClickListener() {

		public void onClick(View v) {
				Intent lostIntent = new Intent(FarePaymentActivity.this, ArrivalsGrid.class);
				
				Bundle b = new Bundle();
				b.putString("stopId", "14572");
				b.putBoolean("fromNfc", true);
				
				lostIntent.putExtras(b);
				
				startActivity(lostIntent);
				d.dismiss();
				finish();
		}
	});

	no.setOnClickListener(new View.OnClickListener() {

		public void onClick(View v) {
				Intent lostIntent = new Intent(FarePaymentActivity.this, BusTimeMainActivity.class);
				startActivity(lostIntent);
				d.dismiss();
				FarePaymentActivity.this.finish();	
		}
	});
	
}
	        
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_nfc, menu);
        return true;
    }


    
}
